// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { Button } from "std-widgets.slint";



// reimplemtns ComboBox so it doesn't depend on the style
// (taken from the printerdemo)
export component ComboBox inherits Rectangle {
    in-out property <string> value: "aaaaaa";
    in property <[string]> model: ["aaaaaa", "bbbbbb", "cccccc"];
    callback selected(string);
    border-radius: 3px;
    border-width: 2px;
    border-color: yellow;
    height: 35px;
    min-width: label.x + label.width + i.width;

    label := Text {
        vertical-alignment: center;
        horizontal-alignment: left;
        text <=> root.value;
        height: 100%;
        x: 12px;
    }

    i := Text {
        width: self.preferred-width;
        x: parent.width - self.width - 5px;
        text: "▼";
    }

    TouchArea {
        clicked => { popup.show(); }

        width: 100%;
        height: 100%;
    }

    popup := PopupWindow {
        x:0;
        y: root.height;
        width: root.width;

        Rectangle {
            background: #eee;
            border-radius: 3px;
            border-width: 2px;
            border-color: yellow;
        }

        VerticalLayout {
            spacing: 6px;
            padding: 3px;

            for value in root.model: Rectangle {
                border-radius: 3px;
                background: item-area.has-hover ? #eef : #fff;
                height: 25px;

                HorizontalLayout {
                    Text {
                        horizontal-alignment: center;
                        vertical-alignment: center;
                        text: value;
                        color: black;
                    }
                }

                item-area := TouchArea {
                    clicked => {
                        root.value = value;
                        root.selected(value);
                    }
                }
            }
        }
    }
}

export component TestCase inherits Window {
    width: 400px;
    height: 400px;

    in-out property <string> result;

    TouchArea {
        clicked => { result += "Root"; }
    }


    VerticalLayout {
        alignment: start;
        HorizontalLayout {
            Button {
                text: "Open Popup1";
                clicked => {
                    popup1.show();
                }
            }
            Button {
                text: "Open Popup2";
                clicked => {
                    popup2.show();
                }
            }
        }
    }

    popup1 := PopupWindow {
        x: 200px; y: 50px;
        Rectangle {
            border-width: 1px;
            border-color: green;
            background: red;
        }
        VerticalLayout {
            padding: 3px;
            ComboBox {
                model: ["First", "Second", "Third"];
                selected(value) => {
                    result += value;
                }
            }
            TouchArea {
                height: 20px;
                clicked => {
                    result += "P1";
                }
            }
        }
        close-policy: close-on-click-outside;
    }

    popup2 := PopupWindow {
        x: 20px; y: 200px;
        width: 350px;
        Rectangle {
            border-width: 1px;
            border-color: pink;
            background: orange;
        }

        VerticalLayout {
            padding: 3px;
            ComboBox {
                model: ["Aaaa", "Bbbb", "Cccc"];
                selected(value) => {
                    result += value;
                }
            }

            HorizontalLayout {
                Button {
                    text: "close this";
                    clicked => {
                        result += "C2";
                        popup2.close();
                    }
                }

                Button {
                    text: "open popup1";
                    clicked => {
                        result += "O1";
                        popup1.show();
                    }
                }
            }
        }

        close-policy: no-auto-close;
    }
}
/*

```rust
#[allow(unused)]
use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
let instance = TestCase::new().unwrap();

// open popup1
slint_testing::send_mouse_click(&instance, 10., 10.);

// check P1 is open
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq!(instance.get_result(), "P1");
instance.set_result("".into());

// click outside
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq!(instance.get_result(), "");
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq!(instance.get_result(), "Root");
instance.set_result("".into());

// open popup2
slint_testing::send_mouse_click(&instance, 210., 10.);
// go in the popup2 combobox
slint_testing::send_mouse_click(&instance, 40., 210.);
slint_testing::send_mouse_click(&instance, 40., 210. + 40.);
assert_eq!(instance.get_result(), "Aaaa");
instance.set_result("".into());

// "close this"
slint_testing::send_mouse_click(&instance, 40., 210. + 40.);
assert_eq!(instance.get_result(), "C2");
instance.set_result("".into());

// open popup1
slint_testing::send_mouse_click(&instance, 10., 10.);

// open P1 combobox and select first
slint_testing::send_mouse_click(&instance, 210., 60.);
slint_testing::send_mouse_click(&instance, 210., 60. + 40.);
assert_eq!(instance.get_result(), "First");
instance.set_result("".into());

// open it again
slint_testing::send_mouse_click(&instance, 210., 60.);

// click outside closes the combobox
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq!(instance.get_result(), "");

// but P1 is still open
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq!(instance.get_result(), "P1");
instance.set_result("".into());

// click outside closes P1
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq!(instance.get_result(), "");

// open popup2
slint_testing::send_mouse_click(&instance, 210., 10.);
assert_eq!(instance.get_result(), "");

// open its combobox
slint_testing::send_mouse_click(&instance, 40., 210.);

// click outside closes it
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq!(instance.get_result(), "");

// but P2 is still open, so open its combobox again
slint_testing::send_mouse_click(&instance, 40., 210.);
slint_testing::send_mouse_click(&instance, 40., 210. + 40.);
assert_eq!(instance.get_result(), "Aaaa");
instance.set_result("".into());

// open popup1 from popup2
slint_testing::send_mouse_click(&instance, 300., 210. + 40.);
assert_eq!(instance.get_result(), "O1");
instance.set_result("".into());

// check P1 is open
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq!(instance.get_result(), "P1");
instance.set_result("".into());

// by opening P1, P2 should be automatically closed
slint_testing::send_mouse_click(&instance, 40., 210.);
assert_eq!(instance.get_result(), "");
slint_testing::send_mouse_click(&instance, 40., 210.);
assert_eq!(instance.get_result(), "Root");
instance.set_result("".into());

// open popup1
slint_testing::send_mouse_click(&instance, 10., 10.);

// close popup1 by esc
slint_testing::send_keyboard_string_sequence(&instance, "\u{001b}");

// check P1 is closed
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq!(instance.get_result(), "Root");
instance.set_result("".into());
```

```cpp
auto handle = TestCase::create();
TestCase &instance = *handle;

// open popup1
slint_testing::send_mouse_click(&instance, 10., 10.);

// check P1 is open
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq(instance.get_result(), "P1");
instance.set_result("");

// click outside
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq(instance.get_result(), "");
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq(instance.get_result(), "Root");
instance.set_result("");

// open popup2
slint_testing::send_mouse_click(&instance, 210., 10.);
// go in the popup2 combobox
slint_testing::send_mouse_click(&instance, 40., 210.);
slint_testing::send_mouse_click(&instance, 40., 210. + 40.);
assert_eq(instance.get_result(), "Aaaa");
instance.set_result("");

// "close this"
slint_testing::send_mouse_click(&instance, 40., 210. + 40.);
assert_eq(instance.get_result(), "C2");
instance.set_result("");

// open popup1
slint_testing::send_mouse_click(&instance, 10., 10.);

// open P1 combobox and select first
slint_testing::send_mouse_click(&instance, 210., 60.);
slint_testing::send_mouse_click(&instance, 210., 60. + 40.);
assert_eq(instance.get_result(), "First");
instance.set_result("");

// open it again
slint_testing::send_mouse_click(&instance, 210., 60.);

// click outside closes the combobox
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq(instance.get_result(), "");

// but P1 is still open
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq(instance.get_result(), "P1");
instance.set_result("");

// click outside closes P1
slint_testing::send_mouse_click(&instance, 20., 310.);
assert_eq(instance.get_result(), "");

// open popup2
slint_testing::send_mouse_click(&instance, 210., 10.);
assert_eq(instance.get_result(), "");

// open its combobox
slint_testing::send_mouse_click(&instance, 40., 210.);

// click outside closes it
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq(instance.get_result(), "");

// but P2 is still open, so open its combobox again
slint_testing::send_mouse_click(&instance, 40., 210.);
slint_testing::send_mouse_click(&instance, 40., 210. + 40.);
assert_eq(instance.get_result(), "Aaaa");
instance.set_result("");

// open popup1 from popup2
slint_testing::send_mouse_click(&instance, 300., 210. + 40.);
assert_eq(instance.get_result(), "O1");
instance.set_result("");

// check P1 is open
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq(instance.get_result(), "P1");
instance.set_result("");

// by opening P1, P2 should be automatically closed
slint_testing::send_mouse_click(&instance, 40., 210.);
assert_eq(instance.get_result(), "");
slint_testing::send_mouse_click(&instance, 40., 210.);
assert_eq(instance.get_result(), "Root");
instance.set_result("");

// open popup1
slint_testing::send_mouse_click(&instance, 10., 10.);

// close popup1 by esc
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::Escape);

// check P1 is closed
slint_testing::send_mouse_click(&instance, 210., 90.);
assert_eq(instance.get_result(), "Root");
instance.set_result("");
```

*/
